<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isRoleshow = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="RoleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                layout="prev,pager,next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formDataCompany.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formDataCompany.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formDataCompany.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formDataCompany.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <!--角色 弹层组件 -->
        <el-dialog v-model="isRoleshow" :visible="isRoleshow" v-bind="$attrs" :title="formData.id ? '编辑': '新增' " v-on="$listeners" @open="onOpen" @close="onClose">
          <el-form ref="formRef" :model="formData" :rules="rules" size="medium" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="formData.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}" />
            </el-form-item>
          </el-form>
          <div slot="footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="handelConfirm">确定</el-button>
          </div>
        </el-dialog>
      </el-card>
      <!-- 分配权限 弹层 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getCompanyInfoAPI, getRoleByidAPI, updateRoleAPI, addRoleAPI } from '@/api/setting'

import { tranListToTreeData } from '@/utils'
import { getRoleDetailByIdAPI, getPermissionList, assignPermAPI } from '@/api/permisson'
export default {
  name: 'Setting',
  components: {},
  props: {},
  data() {
    return {
      RoleList: [],
      page: {
        page: 1, // 当前页面索引
        pagesize: 5, // 页容量
        total: 0 // 总条数
      },
      isRoleshow: false, // 角色弹层
      formDataCompany: {},
      formData: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        des: []
      },
      roleFrom: {}, // 一行的角色信息
      showPermDialog: false, // 控制分配权限弹层的显示后者隐藏
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  async created() {
    this.getRoleList()
    this.formDataCompany = await getCompanyInfoAPI(this.$store.state.user.userInfo.companyId)
  },
  mounted() {
  },
  methods: {
    // 获得角色
    async getRoleList() {
      const { rows, total } = await getRoleListAPI(this.page)
      this.RoleList = rows
      this.page.total = total
    },
    // 切换页面
    handleCurrentChange(newpage) {
      this.page.page = newpage
      this.getRoleList(this.page)
    },
    onOpen() {},
    // 弹层 点插
    onClose() {
      this.isRoleshow = false
      this.$refs['formRef'].resetFields()
      this.$emit('update:visible', false)
      this.formData = {}
    },
    // 弹层 取消按钮
    close() {
      this.onClose()
    },
    // 弹层 确认按钮
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        try {
          if (this.formData.id) {
          // 编辑 提交数据
            await updateRoleAPI(this.formData)
          } else {
            console.log(1)
            await addRoleAPI(this.formData)
          }
          this.$message.success('操作成功')
        } catch (e) {
          console.log(e)
        }
        this.getRoleList() // 刷新列表
        this.onClose() // 关闭对话框
      })
    },
    // 编辑按钮-------------
    async handleEdit(row) {
      console.log(row)
      this.formData = await getRoleByidAPI(row.id)
      this.isRoleshow = true // 打开弹层
    },
    // 权限分配 取消按钮
    btnPermCancel() {
      this.showPermDialog = false
    },
    // 权限分配 确认按钮
    async btnPermOK() {
      await assignPermAPI({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    // 分配权限按钮
    async assignPerm(id) {
      // 请求所有权限, 转化为树形
      this.permData = tranListToTreeData(await getPermissionList(), '0') // 转化list到树形数据
      this.roleId = id
      // 应该去获取 这个id的 权限点
      // 有id 就可以 id应该先记录下来
      const { permIds } = await getRoleDetailByIdAPI(id) // permIds是当前角色所拥有的权限点数据
      // console.log(permIds)
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
